var Md5 = require('../../utils/md5.js');
var timeSecond = Math.floor(Date.now() * 0.001);  //获取时间戳: 
var SignInfoData = Md5.hexMD5(timeSecond + 'nativetalk')//md5加密;
var currentTabIdx = 350;
var classTypeIdxs = 0;
var otherBtnIdxs = 0;
var PageNum = 1;
var teaInforArr = [];
const app = getApp();
var httpNativetalk = app.globalData.httpNativetalk;
var httpWeike = app.globalData.httpWeike;
var teacherDataTotalPage, tokenData;
Page({
  data: {
    typeTitles: ['外教', '中教'],
    currentTab: 0,
    flag: true,
    flags: true,
    classBtnIdx: -1,
    otherType: ['不限', '授课时长', '老师好评'],
    otherBtnIdx: 0,
    imgUrl: 'http://file.waijiaojun.com/waijiaojun/icon',
    hotPeopleFlag: false,
  },  
  onShow: function (e) {
    wx.showShareMenu({
      withShareTicket: true,    
    })
  },
  onLoad: function (e) {
    var that = this;
    //当红人气王的数据请求
    wx.request({
      url: httpNativetalk + '/app/14/index/teacher/recommend',
      data: {},
      header: {//请求头
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        "token": '',
      },
      method: 'POST',
      success: function (res) {
        var hotPeoplesData = res.data.obj;
        if (hotPeoplesData.length !== 0) {
          that.setData({
            hotPeoplesData: hotPeoplesData,
          })
        } else {
          that.setData({
            hotPeopleFlag: true,
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    //首页为您推荐数据请求
    that.recomAreaFun(PageNum);
    //即将上课的数据请求
    that.unfinishData();
  },
  //头部input框
  Form: function (e) {
    wx.navigateTo({
      url: "/pages/search/search",
    });
  },
  //点击了解外教君
  knowAreaFun: function (e) {
    wx.navigateTo({
      url: "/pages/knowVideoPlay/knowVideoPlay",
    });
  },
  //立即体验
  tryAreaFun: function (e) {
    if (app.globalData.token == null) {
      wx.navigateTo({
        url: '/pages/bindPhone/bindPhone',
      })
    }
    if (app.globalData.token !== null) {
      wx.navigateTo({
        url: "/pages/tryClass/tryClass",
      });
    }
  },
  //我要预约
  appointAreaFun: function (e) {
    if (app.globalData.token == null) {
      wx.navigateTo({
        url: '/pages/bindPhone/bindPhone',
      })
    }
    if (app.globalData.token !== null) {
      wx.switchTab({
        url: '/pages/sySelectClass/sySelectClass'
      })
    }
  },
  //即将上课
  immediatelyClassFun: function (e) {
    if (app.globalData.token == null) {
      wx.navigateTo({
        url: '/pages/bindPhone/bindPhone',
      })
    }
    if (app.globalData.token !== null) {
      wx.navigateTo({
        url: "/pages/myAppoint/myAppoint",
      });
    }
  },
  //即将上课时间的数据请求,  //待上课,可取消数据的请求
  unfinishData: function () {
    var that = this;
    wx.login({
      success: res => {
        // console.log(res.code);
        if (res.code !== '') {
          wx.request({
            url: httpWeike + '/wechatSmallProgram/getTokenByCode?code=' + res.code,
            data: {},
            header: {
              "Timestamp": timeSecond,
              "SignInfo": SignInfoData,
              "content-Type": "application/json",
              "client_type": "3",
              "version": 15,
            },
            method: 'GET',
            success: function (res) {
              if (res.data.obj.exist == 1) {
                app.globalData.token = res.data.obj.token;
                tokenData = res.data.obj.token;
                wx.request({
                  url: httpNativetalk + '/app/7/token/teacher/reserve/list/unfinish',
                  data: {
                  },
                  header: {
                    "Timestamp": timeSecond,
                    "SignInfo": SignInfoData,
                    "content-Type": "application/json",
                    "client_type": "3",
                    "version": 15,
                    'token': tokenData,
                  },
                  method: 'POST',
                  success: function (res) {
                    // console.log(res)
                    if (res.data.obj !== null) {
                      var reserveListData = res.data.obj.reserveList;
                      function getDate(time) {
                        var date = new Date(time);
                        var Y = date.getFullYear() + '-';
                        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
                        var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
                        var H = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
                        var Min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
                        return (M + D + ' ' + H + Min);
                      };
                      for (var i = 0; i < reserveListData.length; i++) {
                        var cancleTime = reserveListData[i].reserve_start_time;
                        reserveListData[i].reserve_start_time = getDate(reserveListData[i].reserve_start_time);
                        reserveListData[i].head_portrait = 'http://file.waijiaojun.com/' + reserveListData[i].head_portrait;
                      }
                      that.setData({
                        cancleTime: cancleTime,
                        unfinishListData: reserveListData,
                      })
                    } else {
                      //  djflasdjflajsdfklajsdflkjasldkfj
                      that.setData({
                        appointFlag: true,
                      })
                    }
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              }
              // console.log(res);
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      }
    });
  },
  //教师信息
  teacherInforFun: function (e) {
    var teaIds = e.currentTarget.dataset.idx;
    console.log(teaIds);
    wx.navigateTo({
      url: "/pages/teacherInfor/teacherInfor?teaId=" + teaIds,
    });
  },
  //中教外教的判断
  navbarTap: function (e) {
    var currentTab = e.currentTarget.dataset.idx;
    this.setData({
      currentTab: e.currentTarget.dataset.idx,
    });
    var that = this;
    if (currentTab == 0) {
      currentTabIdx = 350
    } else {
      currentTabIdx = 351
    }
    that.recomAreaFun(PageNum);
  },
  //老师信息的数据请求
  recomAreaFun: function (PageNum) {
    var that = this;
    wx.request({
      url: httpNativetalk + '/app/11/search/index/teacher',
      data: {
        "currentPage": PageNum,
        "categoryId": classTypeIdxs,
        "teacher_language_id": currentTabIdx,
        "dict_id": otherBtnIdxs,     //排序类型，0：综合排序；1：授课时长；2：老师好评
      },
      header: {//请求头
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": '3',
        "version": 15,
      },
      method: "POST",//请求方法
      success: function (res) {
        // console.log(res);
        teacherDataTotalPage = res.data.obj.totalPage
        // console.log(res.data.obj);
        teaInforArr.push(res.data.obj.teachers);
        // console.log(teaInforArr)
        for (var i = 0; i < teaInforArr.length; i++) {
          for (var j = 0; j < teaInforArr[i].length; j++) {
            teaInforArr[i][j].labels = teaInforArr[i][j].labels.slice(0, 3)
          }
        }
        that.setData({
          ForeignTeacherData: res.data.obj.teachers,
          teaInforArr: teaInforArr,
        })
      },
      fail: function (err) {
        console.log(err)
      },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },
  // 课程分类对应ID的函数
  classTypeFun: function (e) {
    classTypeIdxs = e.currentTarget.dataset.idx;
    var classTypeidx = e.currentTarget.dataset.idx;
    // console.log(e.currentTarget.dataset.idx);
    switch (classTypeidx) {
      case undefined:
        classTypeidx = -1;
        break;
      case 1:
        classTypeidx = 0;
        break;
      case 7:
        classTypeidx = 1;
        break;
      case 41:
        classTypeidx = 2;
        break;
      case 34:
        classTypeidx = 3;
        break;
      case 12:
        classTypeidx = 4;
        break;
      case 43:
        classTypeidx = 5;
        break;
      case 42:
        classTypeidx = 6;
        break;
      case 44:
        classTypeidx = 7;
      default:
    };
    this.setData({
      classBtnIdx: classTypeidx,
    });
  },
  //老师筛选 课程分类的数据请求
  selFun: function (e) {
    var that = this;
    wx: wx.request({
      url: httpNativetalk + '/app/15/courseCategoryList',
      data: {},
      header: {
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        "token": '',
      },
      method: 'POST',
      success: function (res) {
        // console.log(res.data.obj.categories);
        that.setData({
          classTypeId: res.data.obj.categories
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    });
    that.setData({
      flag: false,
    })
  },
  // 筛选老师中其他
  otherTypeFun: function (e) {
    otherBtnIdxs = e.currentTarget.dataset.idx;
    this.setData({
      otherBtnIdx: e.currentTarget.dataset.idx,
    });
    // console.log(e.currentTarget.dataset.idx)
  },
  //筛选老师中的确定按钮
  sureBtnFun: function (e) {
    var that = this;
    that.recomAreaFun();
    that.setData({
      flag: true,
    });
  },
  // 上拉触底加载更多
  onReachBottom: function () {
    PageNum++;
    // console.log('这是teaInforArr.length数组的长度=' + teaInforArr.length)
    if (PageNum > 1 && PageNum < teacherDataTotalPage) {
      // console.log('这是第几页?' + PageNum); console.log('这是teaInforArr.length ? ' + teaInforArr.length)
      this.recomAreaFun(PageNum);
    }
    // console.log(teaInforArr)
  },
  onUnload: function () {

  },
  onHide: function () {

  },

})